package com.zxt.dao.impl;

import com.zxt.dao.OrderDao;
import com.zxt.domain.Order;
import com.zxt.domain.OrderDetail;
import com.zxt.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner queryRunner=new QueryRunner();
    @Override
    public void add(Order order) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="insert into tb_order (id,uid,money,status,time,aid) values (?,?,?,?,?,?)";
        Object[] params={order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid()};
        try {
            queryRunner.update(connection,sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public void addDetail(OrderDetail orderDetail) {
        Connection connection = DataSourceUtils.getConnection();
        String sql="insert into tb_orderdetail (oid,pid,num,money) values (?,?,?,?)";
        Object[] params={orderDetail.getOid(),orderDetail.getPid(),orderDetail.getNum(),orderDetail.getMoney()};
        try {
            queryRunner.update(connection,sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public void updateState(String oid, int state) {
        Connection connection = DataSourceUtils.getConnection();
        try {
            queryRunner.update(connection," update tb_order set status=2 where id=? ",oid);
        } catch (SQLException e) {
           throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public List<Order> select(Integer uid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql=" select * from tb_order where uid=? ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Order.class),uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public Order selectByOid(String oid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql=" select * from tb_order where id=? ";
        try {
            return queryRunner.query(connection,sql,new BeanHandler<>(Order.class),oid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }

    @Override
    public List<OrderDetail> selectDetail(String oid) {
        Connection connection = DataSourceUtils.getConnection();
        String sql=" SELECT * FROM tb_orderdetail WHERE oid=? ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(OrderDetail.class),oid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(null,null,connection);
        }
    }


}

